#include "solar/trunk/NPlayMask.h"

NPlayMask::NPlayMask(MaskType Type,
                     int polygon_size,
                     int linear_size,
                     QColor EntercolorForround,
                     QColor LeavecolorForround,
                     QColor EntercolorForshape,
                     QColor LeavecolorForshape,
                     QWidget *parent){

    RdoMap_Color["color-round-to-enter"] = EntercolorForround;
    RdoMap_Color["color-round-to-leave"] = LeavecolorForround;
    RdoMap_Color["Color-roundTodefault"] = LeavecolorForround;

    RdoMap_Color["color-shape-to-enter"] = EntercolorForshape;
    RdoMap_Color["color-shape-to-leave"] = LeavecolorForshape;
    RdoMap_Color["color-shape-to-default"] = LeavecolorForshape;

    RdoMap_Integet["polygon_size"] = polygon_size;
    RdoMap_Integet["linear_size"] = linear_size;

    RdoMap_MaskType["mask-type"] = Type;
    RdoMap_Bool["extra-press"] = false;
    setAlignment(Qt::AlignCenter);
    setParent(parent);
}

NPlayMask::NPlayMask(MaskType Type,
                     int polygon_size,
                     int linear_size,
                     QColor EntercolorForshape,
                     QColor LeavecolorForshape,
                     QWidget *parent){

    RdoMap_Color["color-shape-to-enter"] = EntercolorForshape;
    RdoMap_Color["color-shape-to-leave"] = LeavecolorForshape;
    RdoMap_Color["color-shape-to-default"] = LeavecolorForshape;

    RdoMap_Integet["polygon_size"] = polygon_size;
    RdoMap_Integet["linear_size"] = linear_size;

    RdoMap_MaskType["mask-type"] = Type;
    RdoMap_Bool["extra-press"] = false;
    setAlignment(Qt::AlignCenter);
    setParent(parent);
}

void NPlayMask::d_roundet_color(QColor color){
    RdoMap_Color["color-round-to-enter"] = color;
}

void NPlayMask::d_roundlv_color(QColor color){
    RdoMap_Color["color-round-to-leave"] = color;
    RdoMap_Color["Color-roundTodefault"] = color;
}

void NPlayMask::d_graphet_color(QColor color){
    RdoMap_Color["color-shape-to-enter"] = color;
}

void NPlayMask::d_graphlv_color(QColor color){
    RdoMap_Color["color-shape-to-leave"] = color;
    RdoMap_Color["color-shape-to-default"] = color;
}

void NPlayMask::d_polygon_size(int size){
    RdoMap_Integet["polygon_size"] = size;
}

void NPlayMask::d_linear_size(int size){
    RdoMap_Integet["linear_size"] = size;
}

void NPlayMask::enterEvent(QEnterEvent*){
    RdoMap_Color["Color-roundTodefault"] = RdoMap_Color["color-round-to-enter"];
    RdoMap_Color["color-shape-to-default"] = RdoMap_Color["color-shape-to-enter"];
    update();
}

void NPlayMask::leaveEvent(QEvent*){
    RdoMap_Color["Color-roundTodefault"] = RdoMap_Color["color-round-to-leave"];
    RdoMap_Color["color-shape-to-default"] = RdoMap_Color["color-shape-to-leave"];
    update();
}

void NPlayMask::mousePressEvent(QMouseEvent*){
    RdoMap_Integet["polygon_size"] += 1;
    emit msev_pre();
    update();
}

void NPlayMask::mouseReleaseEvent(QMouseEvent*){
    RdoMap_Integet["polygon_size"] -= 1;
    emit msev_rls();
    update();
}

void NPlayMask::resizeEvent(QResizeEvent *event){
    RdoMap_Integet[RPLKEY.WIN_SIZE_W] = event->size().width();
    RdoMap_Integet[RPLKEY.WIN_SIZE_H] = event->size().height();
    QLabel::resizeEvent(event);
}

void NPlayMask::setExtraPree(PlayerDecide decide){
    if (decide == PlayerDecide::UnDecide) {
        if (RdoMap_Bool["extra-press"]){
            RdoMap_Bool["extra-press"] = false;
        } else {
            RdoMap_Bool["extra-press"] = true;
        }
    } else {
        if (decide == PlayerDecide::DecideTrue) {
            RdoMap_Bool["extra-press"] = true;
        }
        if (decide == PlayerDecide::DecideFalse) {
            RdoMap_Bool["extra-press"] = false;
        }
    }
    update();
}

void NPlayMask::paintEvent(QPaintEvent*){
    QPainter CaptainBrush(this);
    CaptainBrush.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    int ShapeSize = RdoMap_Integet["polygon_size"];
    int linear_size = RdoMap_Integet["Linear_Size"];
    int Width = RdoMap_Integet[RPLKEY.WIN_SIZE_W];
    int Height = RdoMap_Integet[RPLKEY.WIN_SIZE_H];
    int HalfWidth = RdoMap_Integet[RPLKEY.WIN_SIZE_W] / 2;
    int HalfHeight = RdoMap_Integet[RPLKEY.WIN_SIZE_H] / 2;
    QPoint Left_Roof = QPoint(HalfWidth - ShapeSize + (ShapeSize / 2), HalfHeight - ShapeSize);
    QPoint Left_Botton = QPoint(HalfWidth - ShapeSize + (ShapeSize / 2), HalfHeight + ShapeSize);
    QPoint Right_Roof = QPoint(HalfWidth + ShapeSize - (ShapeSize / 2), HalfHeight - ShapeSize);
    QPoint Right_Botton = QPoint(HalfWidth + ShapeSize - (ShapeSize / 2), HalfHeight + ShapeSize);

    QPen LinearPen = QPen(RdoMap_Color["color-shape-to-default"]);
    LinearPen.setWidth(linear_size);
    QBrush PolygonBrush = QBrush(RdoMap_Color["color-shape-to-default"]);

    if (RdoMap_MaskType["mask-type"] == MaskType::PlayButton){
        QBrush round_brush = QBrush(RdoMap_Color["Color-roundTodefault"]);
        CaptainBrush.setBrush(round_brush);
        CaptainBrush.setPen(Qt::NoPen);
        QRect roundrect = QRect(0, 0, Width, Height);
        CaptainBrush.drawRoundedRect(roundrect, HalfWidth, HalfHeight, Qt::AbsoluteSize);
        if (RdoMap_Bool["extra-press"]){
            CaptainBrush.setPen(LinearPen);
            QList<std::pair<QPoint, QPoint>> LinePoint ({std::make_pair(Left_Roof, Left_Botton),
                                                    std::make_pair(Right_Roof, Right_Botton)});
            foreach(auto I, LinePoint){
                CaptainBrush.drawLine(I.first, I.second);
            }
        } else {
            CaptainBrush.setBrush(PolygonBrush);
            QPoint TrianglePoint = QPoint(HalfWidth + ShapeSize, HalfHeight);
            QPolygonF PolygonF({Left_Roof, Left_Botton, TrianglePoint});
            CaptainBrush.drawPolygon(PolygonF);
        }
    } else if (QList<MaskType>({MaskType::NextButton, MaskType::PriviousButton}).contains(RdoMap_MaskType["mask-type"])){
        CaptainBrush.setPen(Qt::NoPen);
        if (RdoMap_MaskType["mask-type"] == MaskType::PriviousButton){
            CaptainBrush.translate(Width, Height);
            CaptainBrush.rotate(180);
        }
        //组件宽-图形尺寸-预留美化宽-线条宽，平分后为起始绘画点
        int IdeaSurplus = (Width - ShapeSize * 3 + ShapeSize / 4 - linear_size) / 2;
        QPoint FirstRoof = QPoint(IdeaSurplus, HalfHeight - ShapeSize);
        QPoint FirstBotton = QPoint(IdeaSurplus, HalfHeight + ShapeSize);
        QPoint FirstPivot = QPoint(IdeaSurplus + ShapeSize * 2, HalfHeight);
        //第二个三角形超出第一个三角形一半多一点点，比较美观
        int SecondStart = IdeaSurplus + ShapeSize + ShapeSize / 4;
        QPoint SecondRoof = QPoint(SecondStart, HalfHeight - ShapeSize);
        QPoint SecondBotton = QPoint(SecondStart, HalfHeight + ShapeSize);
        QPoint SecondPivot = QPoint(IdeaSurplus + ShapeSize * 3, HalfHeight);
        QList<QPoint> TriangleFirst({FirstRoof, FirstBotton, FirstPivot});
        QList<QPoint> TriangleSecond({SecondRoof, SecondBotton, SecondPivot});
        QList<QList<QPoint>> Triangle({TriangleFirst, TriangleSecond});
        CaptainBrush.setBrush(RdoMap_Color["color-shape-to-default"]);
        foreach(auto I, Triangle){
            CaptainBrush.drawPolygon(QPolygonF(I.toVector()));
        }
        CaptainBrush.setPen(LinearPen);
        int lineForroof = SecondPivot.x() - linear_size / 2;
        int First_Y = HalfHeight - ShapeSize;
        int Second_Y = HalfHeight + ShapeSize;
        CaptainBrush.drawLine(QPoint(lineForroof, First_Y), QPoint(lineForroof, Second_Y));
    }
    CaptainBrush.end();
}
